using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using PropertyManagement.Core.Model;

namespace PropertyManagement.Core.DataAccess {
        public static partial class FeeUnitPriceManageDataAccess {
             
            public static FeeUnitPriceManage GetById(int id){
                var sql = @"select * from dbo.FeeUnitPriceManage where FeeUnitPriceManageID = @id ";
                var paramList = new[] {
                    new SqlParameter("id", id)
                };
                using (var iReader = SqlServerHelper.ExecuteReader(SqlServerHelper.ConnectionString, CommandType.Text, sql,
                                                                paramList)) {
                    if (iReader.Read( )) {
                            return CreationFeeUnitPriceManageByIReader(iReader);
                    };
                    return null;
                }
            }

            public static void Add(FeeUnitPriceManage entity){
                  var sql = @"insert into dbo.FeeUnitPriceManage values (@billtype,@unitprice,@isexpire,@creationdate,@lastupdate,@creationuserid,@lastactionuserid) ;   select SCOPE_IDENTITY (); ";
                  var paramList = new SqlParameter[] {
							new SqlParameter("billtype",entity.BillType),
							new SqlParameter("creationdate",entity.CreationDate),
							new SqlParameter("creationuserid",entity.CreationUserID),
							new SqlParameter("isexpire",entity.IsExpire),
							new SqlParameter("lastactionuserid",entity.LastActionUserID),
							new SqlParameter("lastupdate",entity.LastUpdate),
							new SqlParameter("unitprice",entity.UnitPrice)
                        };
                  using ( var ireader = SqlServerHelper.ExecuteReader(SqlServerHelper.ConnectionString, CommandType.Text, sql,
                                                            paramList)) {
                        if (ireader.Read()) {
                            entity.FeeUnitPriceManageID = int.Parse(ireader[0].ToString());
                        }
                  }
            
            }   

            public static void Remove(FeeUnitPriceManage entity) {
                var sql = @"delete from dbo.FeeUnitPriceManage where FeeUnitPriceManageID = @id";
                var paramList = new[] {
                    new SqlParameter("id", entity.FeeUnitPriceManageID)
                };
                SqlServerHelper.ExecuteNonQuery(SqlServerHelper.ConnectionString, CommandType.Text, sql, paramList);
            }   

            public static IEnumerable<FeeUnitPriceManage> LoadAll(){
                    var sql = " select * from dbo.FeeUnitPriceManage ";
                    var list = new List<FeeUnitPriceManage>( );
                    using (var iReader = SqlServerHelper.ExecuteReader(SqlServerHelper.ConnectionString, CommandType.Text, sql)) {
                        while (iReader.Read( )) {
                                list.Add(CreationFeeUnitPriceManageByIReader(iReader));
                        };
                    }  
                    return list;
            }   
        
            public static void Update(FeeUnitPriceManage entity){
                var sql = @" update dbo.FeeUnitPriceManage set BillType=@billtype,CreationDate=@creationdate,CreationUserID=@creationuserid,IsExpire=@isexpire,LastActionUserID=@lastactionuserid,LastUpdate=@lastupdate,UnitPrice=@unitprice where FeeUnitPriceManageID = @id ";
                var paramList = new SqlParameter[] {
                            new SqlParameter("id",entity.FeeUnitPriceManageID),
							new SqlParameter("billtype",entity.BillType),
							new SqlParameter("creationdate",entity.CreationDate),
							new SqlParameter("creationuserid",entity.CreationUserID),
							new SqlParameter("isexpire",entity.IsExpire),
							new SqlParameter("lastactionuserid",entity.LastActionUserID),
							new SqlParameter("lastupdate",entity.LastUpdate),
							new SqlParameter("unitprice",entity.UnitPrice)
                        };
                SqlServerHelper.ExecuteNonQuery(SqlServerHelper.ConnectionString, CommandType.Text, sql, paramList);
            }
            
            public static bool Exists(FeeUnitPriceManage entity){
                return entity.FeeUnitPriceManageID != 0 && GetById(entity.FeeUnitPriceManageID)!=null;
            }


            private static FeeUnitPriceManage CreationFeeUnitPriceManageByIReader(IDataReader iReader){
                if(iReader!=null){
                      return new FeeUnitPriceManage{
							BillType = iReader["BillType"] is DBNull ? default(BillType) : (BillType)iReader["BillType"] ,
							CreationDate = iReader["CreationDate"] is DBNull ? default(DateTime) : (DateTime)iReader["CreationDate"] ,
							CreationUserID = iReader["CreationUserID"] is DBNull ? default(Int32) : (Int32)iReader["CreationUserID"] ,
							FeeUnitPriceManageID = iReader["FeeUnitPriceManageID"] is DBNull ? default(Int32) : (Int32)iReader["FeeUnitPriceManageID"] ,
							IsExpire = iReader["IsExpire"] is DBNull ? default(Boolean) : (Boolean)iReader["IsExpire"] ,
							LastActionUserID = iReader["LastActionUserID"] is DBNull ? default(Int32) : (Int32)iReader["LastActionUserID"] ,
							LastUpdate = iReader["LastUpdate"] is DBNull ? default(DateTime) : (DateTime)iReader["LastUpdate"] ,
							UnitPrice = iReader["UnitPrice"] is DBNull ? default(Decimal) : (Decimal)iReader["UnitPrice"] 
                        };
                }
                return null; 
            }

            
    }
}